package middlewares

import (
	"gin-demo/controllers/admin"
	"gin-demo/utils/jwt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AuthHandler(c *gin.Context) {
	var user admin.Login
	err := c.ShouldBind(&user)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 2001,
			"msg":  "无效的参数 ",
		})
	}
	if user.Username == "admin" && user.Password == "123456" {
		// 生成token
		tokenString, _ := jwt.GenToken(user.Username)
		c.JSON(http.StatusOK, gin.H{
			"code": 2000,
			"msg":  "success",
			"data": gin.H{"token": tokenString},
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 2002,
		"msg":  "鉴权失败",
	})
}
